<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // promise的终极调用方案 ：ES8出现。
        // async 及 await ： 改变了 promise的then的调用形式，相当于通过同步来调用异步代码

        // 依次调用 3个promise
        function fn1() {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    console.log(1)
                    resolve(111)
                }, 1000);
            })
        }

        function fn2() {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    console.log(2)
                    // resolve(222)
                    reject("22失败")

                }, 1000);
            })
        }

        function fn3() {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    console.log(3);
                    resolve(333)
                    // reject("错误了");
                }, 1000);
            })
        }
        async function myfn() {
            try {
                let res1 = await fn1()
                console.log(res1);
                let res2 = await fn2()
                console.log(res2,'res222');
                let res3 = await fn3()
                console.log(res3)
            } catch (error) {
                console.log(error,'err');//reject会走到catch(err)里面，后面的就不会再执行
            }

        }
        myfn()
    </script>
</body>

</html>